package com.sharemarking.application.common.service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.sharemarking.application.common.entity.Company;
import com.sharemarking.application.common.entity.Employee;
import com.sharemarking.application.common.repository.CompanyRepo;
import com.sharemarking.application.common.repository.EmployeeRepo;
import com.sharemarking.application.common.util.Util;


@Controller
@RequestMapping("phone")
public class CompanyCon {
	
	@Autowired
	private CompanyRepo dao;
	@Autowired
	private EmployeeRepo dao1;
	
	@RequestMapping(value="getAllCompany")
	@ResponseBody
	public List<Company> getAllDepart(Company e,Integer id){
		
		List<Company> departs=(List<Company>) dao.findAll();
		List<Company> list=new ArrayList<Company>();
		Employee emp=dao1.findOne(id);
		for(int i=0;i<departs.size();i++){
			if(departs.get(i).getId()==emp.getCompanyId()){
				list.add(departs.get(i));
			}
		}
		//System.out.println(list);
		//System.out.println(departs);
		return list;
	}
	@RequestMapping(value="AddCompany")
	
	public String AddCompany(Company company,MultipartHttpServletRequest req,HttpServletResponse resp){
		//上传图片再保存
		//生成公司二维码
		try{
			MultipartFile file=req.getFile("photo");
			String fileName=file.getOriginalFilename();
			InputStream isRef=file.getInputStream();
			String targetDir=req.getSession().getServletContext().getRealPath("ipload");
			File targetFile=new File(targetDir,fileName);
			FileOutputStream fodef=new FileOutputStream(targetFile);
			IOUtils.copy(isRef, fodef);
			
			dao.save(company);
			
			
		}catch(Exception e){
			e.printStackTrace();
		}	
		return "success";
	}
	
	
	@RequestMapping(value="UpdateCompany")
	@ResponseBody
	public boolean UpdateCompany(HttpServletResponse resp,String describer,Integer id,String tel,String address,String photo){
		
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("UTF-8");
		Date d=new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String imgName=sdf.format(d);
		String path = this.getClass().getClassLoader().getResource("").getPath()+"public/uploadImage/companyLogo/";
		System.out.println(path);
		String path2 = this.getClass().getClassLoader().getResource("").getPath()+"public/uploadImage/companyLogo";
		File file=new File(path2);
		file.mkdirs();
		Util.GenerateImage(photo,path+imgName+".jpg");
		Company com=dao.findOne(id);
		com.setPhoto(imgName+".jpg");
		com.setTel(tel);
		com.setDescriber(describer);
		com.setAddress(address);
		dao.save(com);
		return true;
		
	}
	
	@RequestMapping(value="getCode")
	public void getCore(HttpServletResponse response)throws ServletException, IOException{
		 
		ServletOutputStream stream = null;;
        try {  
            stream = response.getOutputStream();  
            QRCodeWriter writer = new QRCodeWriter();  
            BitMatrix matrix = writer.encode("ok", BarcodeFormat.QR_CODE, 200, 200);
            String path = this.getClass().getClassLoader().getResource("").getPath()+"public/uploadImage/companyLogo/";
            System.out.println(path);
            
            File codeFile=new File(path+"code.png");
            MatrixToImageWriter.writeToFile(matrix, "png", codeFile); 
            System.out.println("ok");
        } catch (WriterException e) {  
            e.printStackTrace(); 
            System.out.println("no");
        } finally {  
            if (stream != null) {  
                stream.flush();  
                stream.close();  
            }  
        }  
	        
	 
	}
	
	
}
